package com.hjw.controller.adm;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.hjw.entity.PasswordEncoder;
import com.hjw.entity.User;
import com.hjw.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @创建时间：2025-04-25
 * @文件名称：satoken_sobuk - com.hjw.controller.adm
 */
@SaCheckRole("adm")
@Slf4j
@RestController
@RequestMapping("/api/adm/user")
public class AdmUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("/findAll") // /api/adm/user/findAll
    public SaResult findAll() {
        List<User> all = userService.findAll();
        log.info("管理员查询所有用户信息：{}",all);
        return SaResult.get(200,"查询成功",all);
    }

    @PostMapping("/updatePwd")
    public SaResult updatePwd(@RequestBody Map<String,Object> map){
        log.info("管理员修改用户密码请求参数：{}", map);
        if (!map.containsKey("id") || !map.containsKey("pwd")){
            return SaResult.get(-200,"用户编号不存在", null);
        }
        String id = map.get("id").toString();
        String pwd = map.get("pwd").toString();
        User adm = userService.findById(StpUtil.getLoginIdAsInt());

        if (adm.getId() == Integer.parseInt(id)){
            return SaResult.get(-200,"不可修改超管用户信息", null);
        }
        if (!passwordEncoder.matches(pwd,adm.getPassword())){
            return SaResult.get(-200,"管理员秘钥错误", null);
        }
        String pwdEncode = passwordEncoder.encode("123456");
        map.put("password",pwdEncode);

        int update = userService.update(map);
        if (update == 0){
            return SaResult.get(-200,"修改失败", null);
        }
        return SaResult.get(200,"修改成功", null);
    }

    @PostMapping("/Restart")
    public SaResult Restart(@RequestBody Map<String,Object> map){
        log.info("管理员修改用户信息请求参数：{}", map);
        if (!map.containsKey("id")){
            return SaResult.get(-200,"用户编号不存在", null);
        }
        User adm = userService.findById(StpUtil.getLoginIdAsInt());
        if (adm.getId() == Integer.parseInt(map.get("id").toString())){
            return SaResult.get(-200,"不可修改本身用户信息", null);
        }
        if (!adm.getAccount().equals("adm")){
            return SaResult.get(-200,"用户非超管账户，无法修改此项", null);
        }
        int update = userService.update(map);
        if (update == 0){
            return SaResult.get(-200,"修改失败", null);
        }
        return SaResult.get(200,"修改成功", null);
    }

    @GetMapping("/findAdm")// /api/adm/user/findAdm
    public SaResult findAdm(){
        Map<String, Object> m = new HashMap<>(1);
        m.put("role","adm");
        List<User> users = userService.findByCondition(m);
        return SaResult.get(200,"查询成功",users);
    }

    @PostMapping("/addAdm")
    public SaResult addAdm(@RequestBody Map<String, Object> map){
        if (!map.containsKey("account") || !map.containsKey("pwd") || !map.containsKey("nickname")){
            return SaResult.get(-200,"用户信息不完整", null);
        }
        User adm = userService.findById(StpUtil.getLoginIdAsInt());
        if (!passwordEncoder.matches(map.get("pwd").toString(),adm.getPassword())){
            return SaResult.get(-200,"管理员秘钥错误", null);
        }
        User user = new User();
        user.setAccount(map.get("account").toString());
        user.setPassword(passwordEncoder.encode("123456"));
        user.setRole("adm");
        user.setPhone(null);
        user.setNickname(map.get("nickname").toString());
        user.setClosed(0);
        int add = userService.add(user);
        if (add == 0){
            return SaResult.get(-200,"添加失败", null);
        }
        Map<String, Object> m = new HashMap<>(1);
        m.put("role","adm");
        List<User> users = userService.findByCondition(m);
        return SaResult.get(200,"添加成功",users);
    }

    @GetMapping("/deleteAdm/{id}")
    public SaResult deleteAdm(@PathVariable("id") Integer id){
        int delete = userService.delete(id);
        if (delete == 0){
            return SaResult.get(-200,"删除失败", null);
        }
        User user = userService.findById(StpUtil.getLoginIdAsInt());
        if (user.getAccount().equals("adm")){
            return SaResult.get(-200,"无超管权限，无法删除管理", null);
        }
        Map<String, Object> m = new HashMap<>(1);
        m.put("role","adm");
        List<User> users = userService.findByCondition(m);
        return SaResult.get(200,"删除成功",users);
    }



}
